package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.WorkflowApply;
import com.hunttown.mes.rpc.domain.WorkflowApplyDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import com.hunttown.mes.rpc.exception.ConvertException;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-08-01 15:48:17
 */
public class WorkflowApplyConverts {

    private static Logger LOG = LoggerFactory.getLogger(WorkflowApplyConverts.class);

    public static List<WorkflowApplyDTO> convert(List<WorkflowApply> workflowApplyList) {
        if (CollectionUtils.isEmpty(workflowApplyList)) {
            return Collections.EMPTY_LIST;
        }

        List<WorkflowApplyDTO> workflowApplyListResult = Lists.newArrayList();
        for (WorkflowApply workflowApply : workflowApplyList) {
            workflowApplyListResult.add(convert(workflowApply));
        }
        return workflowApplyListResult;
    }

    public static WorkflowApplyDTO convert(WorkflowApply workflowApply) {
        try {
            return BeanUtils.copy(workflowApply, WorkflowApplyDTO.class);
        } catch (Exception e) {
            LOG.error("WorkflowApply到WorkflowApplyDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "WorkflowApply到WorkflowApplyDTO转换失败", e);
        }
    }

    public static WorkflowApply convert(WorkflowApplyDTO workflowApplyDTO) {
        try {
            return BeanUtils.copy(workflowApplyDTO, WorkflowApply.class);
        } catch (Exception e) {
            LOG.error("WorkflowApplyDTO到WorkflowApply转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "WorkflowApplyDTO到WorkflowApply转换失败", e);
        }
    }
}